<?php
$server = $_SERVER['SERVER_NAME']; 
$endereco = $_SERVER ['REQUEST_URI'];
//echo "http://" . $endereco;
$enderecoCortado = explode("/", $endereco);
if(in_array("paginas", $enderecoCortado)){
Header("Location: ../login.php");
}
?>
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <h3 class="page-title"> Usuários <small> Lista de Usuários cadastrados</small></h3>
            <ul class="breadcrumb">
                <li><a href="#"><i class="icon-home"></i></a><span class="divider">&nbsp;</span>
                </li>
                <li><a href="#">Usuários</a><span class="divider">&nbsp;</span>
                </li>
                <li><a href="#">Administrar Usuários</a><span class="divider-last">&nbsp;</span>
                </li>
            </ul>
        </div>
    </div>
<a href="gerenciar-usuarios">
<button class="btn btn-success" style="margin-bottom:20px;">
    <i class="icon-plus icon-white"></i> Cadastrar Novo</button>
</a>

<div class="row-fluid">
    <div class="span12">
        <div class="widget">
        <div class="widget-title">
            <h4><i class="icon-reorder"></i> Usuários</h4>
            <span class="tools">
            <a href="javascript:;" class="icon-chevron-down"></a>
            <a href="javascript:;" class="icon-remove"></a>
        </span>
        </div>

        <div class="widget-body">
            <?php
                $sql=mysql_query("SELECT * FROM usuarios") or die("ERRO NO COMANDO SQL");
                $verificaUltimoUsuario = mysql_num_rows($sql);
            ?>

        <table class="table table-striped table-bordered" id="sample_1">
            <thead>
            <tr>
                <th>ID</th>
                <th class="hidden-phone">Nome</th>
                <th class="hidden-phone">E-mail</th>
                <th class="hidden-phone">Usuário</th>
                <th class="hidden-phone">Status</th>
                <th class="hidden-phone">Ações</th>
            </tr>
            </thead>
            <tbody>
                <?php
                while ($linhaConsulta = mysql_fetch_object($sql)) {
                $id = $linhaConsulta->id;
                $nome = $linhaConsulta->nome;
                $email = $linhaConsulta->email;
                $usuario = $linhaConsulta->usuario;
                $status = $linhaConsulta->status;
                ?>
                <tr class="odd gradeX">
                    <td><?php echo $id; ?></td>
                    <td><?php echo utf8_encode($nome); ?></td>
                    <td class="hidden-phone"><a href="mailto:shuxer@gmail.com"><?php echo $email; ?></a>
                    </td>
                    <td class="hidden-phone"><?php echo $usuario ?></td>
                    <td class="hidden-phone">
                        <?php 
                            if($status == 'ativo'){
                        ?>
                        <span class="label label-success">ativo</span>
                        <?php
                        }
                        else{
                        ?>
                        <span class="label label-warning">desativado</span>
                        <?php
                        }
                        ?>
                    </td>
                    <td class="center hidden-phone" width="40">
                        <button class="btn btn-danger btn-mini"><i class="icon-remove icon-white"></i> Excluir</button>
                        <a href="gerenciar-usuarios?id=<?php echo $id;?>"><button class="btn btn-primary btn-mini"><i class="icon-pencil icon-white"></i> Editar</button></a>
                        
                        <div class="btn-group">
                            <a href="#" class="btn"><i class="icon-cog"></i> Ações</a>
                            <a href="#" data-toggle="dropdown" class="btn dropdown-toggle">
                                <span class="icon-caret-down"></span>
                            </a>
                                <ul class="dropdown-menu">
                                    <li>
                                        <a href="perfil">
                                            <i class="icon-plus"></i> Detalhes</a>
                                    </li>
                                    <li>
                                        <a href="gerenciar-usuarios?id=<?php echo $id;?>">
                                        <i class="icon-pencil"></i> Editar</a>
                                    </li>
                                    <?php 
                                        if ($verificaUltimoUsuario > 1) {
                                         ?>
                                        <form action="" method="POST" style="margin:0; padding:0;">
                                    <li>
                                        <a href="administrar-usuarios?acao=deletar&id=<?php echo $id;?>">
                                        <i class="icon-trash"></i> Deletar</a>
                                    </li>
                                    <input type="hidden" value="<?php echo $id;?>" name="id">
                                    </form>
                                    
                                    <?php } ?>
                                </ul>
                        </div>
                    </td>
                </tr>
                <?php
                }
                ?>
               </tbody>
        </table>
        </div>
        </div>                   
    </div>
</div>
<?php
    if(isset($_GET['acao']) && $_GET['acao'] == 'deletar'){
        $id = (int) $_GET['id'];
        $sqlDeletar = "DELETE FROM `usuarios` WHERE `id`='$id'";
        $deletar = mysql_query($sqlDeletar);

        if ($deletar) {
            echo '<script>
            document.location="administrar-usuarios";
            </script>';
            echo 'usuário excluido com sucesso!';
        }
        else{
            echo '<div class="alert alert-error">Erro ao excluir usuário</div>';
        }

    }
?>
</div>
    